// Copyright 2025 祝灵福
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import { Logs } from '../logs/logs.entity'
import { Roles } from '../roles/roles.entity'
import {
  // AfterInsert,
  // AfterRemove,
  // BeforeInsert,
  // BeforeRemove,
  Column,
  Entity,
  JoinTable,
  ManyToMany,
  OneToMany,
  OneToOne,
  PrimaryGeneratedColumn
} from 'typeorm'
import { Profile } from './profile.entity'
import { Exclude } from 'class-transformer'

@Entity()
export class User {
  @PrimaryGeneratedColumn()
  @Exclude()
  id: number
  @Column({ unique: true })
  username: string
  @Column()
  @Exclude()
  password: string
  // 1to1
  @OneToOne(() => Profile, (profile) => profile.user, { cascade: true })
  profile: Profile
  // 1to多
  @OneToMany(() => Logs, (logs) => logs.user, { cascade: true })
  logs: Logs[]
  //   多对多:避免被级联删除，只赋予了级联插入权限
  @ManyToMany(() => Roles, (roles) => roles.user, { cascade: ['insert'] })
  @JoinTable({ name: 'user_roles' })
  roles: Roles[]

  // 生命周期钩子
  // @BeforeInsert()
  // beforeInsert() {
  //   console.log('插入数据之前执行')
  // }
  // @AfterInsert()
  // afterInsert() {
  //   console.log('插入数据之后执行', this.id, this.username)
  // }
  // @BeforeRemove()
  // beforeRemove() {
  //   console.log('删除数据之前执行')
  // }
  // @AfterRemove()
  // afterRemove() {
  //   console.log('删除数据之后执行')
  // }
}
